package com.iot.inclinometer.project.service.Impl;

import com.alibaba.fastjson.JSON;
import com.iot.inclinometer.common.DbConfig;
import com.iot.inclinometer.project.entity.BaseInfo;
import com.iot.inclinometer.project.service.ProjectService;
import com.iot.inclinometer.util.BeanUtil;
import com.iot.inclinometer.util.SQLUtil;

import org.xutils.DbManager;
import org.xutils.db.DbModelSelector;
import org.xutils.db.Selector;
import org.xutils.db.sqlite.SqlInfo;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.DbModel;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;

/**
 * @Author: Song Yu
 * @Date: 2021/4/12 0012 19:19
 */
public class ProjectServiceImpl implements ProjectService {

    DbManager db;

    {
        try {
            db = x.getDb(DbConfig.getDaoConfig());
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @SneakyThrows
    @Override
    public List<BaseInfo> queryList(BaseInfo queryEntity){
        String sqlString = SQLUtil.getBaseQuerySQL("base_info",queryEntity);
        SqlInfo sqlInfo = new SqlInfo(sqlString);
        List<DbModel> dbModel = db.findDbModelAll(sqlInfo);
        List<BaseInfo> result = new ArrayList<>();
        for(DbModel item:dbModel){
            String str = JSON.toJSONString(item.getDataMap());

            BaseInfo baseInfo = JSON.parseObject(str,BaseInfo.class);
            result.add(baseInfo);
        }
        return result;
    }

    @SneakyThrows
    @Override
    public BaseInfo queryOne(BaseInfo queryEntity){
        String sqlString = SQLUtil.getBaseQuerySQL("base_info",queryEntity);
        SqlInfo sqlInfo = new SqlInfo(sqlString);
        DbModel dbModel = db.findDbModelFirst(sqlInfo);
        String str = JSON.toJSONString(dbModel.getDataMap());
        System.out.println(str);
        BaseInfo result = JSON.parseObject(str,BaseInfo.class);
        return result;
    }

    @SneakyThrows
    @Override
    public List<String> queryItemNameByProjectName(String projectName){
        SqlInfo sqlInfo = new SqlInfo("select distinct item_name from base_info where project_name ='"+projectName+"' ");
        List<DbModel> dbModel =db.findDbModelAll(sqlInfo);
        List<String> result = new ArrayList<>();
        for(DbModel item:dbModel){
            result.add(item.getString("item_name"));
        }
        return result;
    }

    @SneakyThrows
    @Override
    public List<String> queryDrillNumByItemName(String projectName,String itemName){
        SqlInfo sqlInfo = new SqlInfo("select distinct drill_num from base_info where project_name ='"+projectName+"' and item_name ='"+itemName+"'");
        List<DbModel> dbModel =db.findDbModelAll(sqlInfo);
        List<String> result = new ArrayList<>();
        for(DbModel item:dbModel){
            result.add(item.getString("drill_num"));
        }
        return result;
    }

    @Override
    public boolean delete(String projectName,String itemName,String drillNum){
        WhereBuilder whereBuilder = WhereBuilder.b();
        whereBuilder.and("project_name","=",projectName);
        whereBuilder.and("item_name","=",itemName);
        whereBuilder.and("drill_num","=",drillNum);
        try {
            db.delete(BaseInfo.class,whereBuilder);
        } catch (DbException e) {
            return false;
        }
        return true;
    }

    @SneakyThrows
    @Override
    public List<String> queryProjectName(){
        SqlInfo sqlInfo = new SqlInfo("select distinct project_name from base_info");
        List<DbModel> dbModel =db.findDbModelAll(sqlInfo);
        List<String> result = new ArrayList<>();
        for(DbModel item:dbModel){
            result.add(item.getString("project_name"));
        }
        return result;
    }

    @SneakyThrows
    @Override
    public boolean addOne(BaseInfo insertEntity) {
        List<BaseInfo> all = db.selector(BaseInfo.class)
                .where("project_name","=",insertEntity.getProject_name())
                .and("item_name","=",insertEntity.getItem_name())
                .and("drill_num","=",insertEntity.getDrill_num()).findAll();
        if(all==null||all.isEmpty()) {
            db.save(insertEntity);
        }else{
            return false;
        }
        return true;
    }
}
